<?php
/**
 * 管理中心· 发送站内短信模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2020 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com;
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Hoteladmin\Controller;
use Think\AjaxPage;
use Hoteladmin\Model\MessageModel;
class MessageController extends BaseController{
    /**
     +--------------------------------------------------------------------------
     * 初始化程序
     +--------------------------------------------------------------------------
     */
    public function _initialize(){
        parent::_initialize();
        $this->mess_type= C('MESS_TYPE');
        $this->assign('mtype',$this->mess_type);  //站内短息类型
    }
	/**
	 +--------------------------------------------------------------------------
	 * 实现管理员发送站内短信的功能
	 * @author:  XC跃
	 * @date :2018-03-14
	 +--------------------------------------------------------------------------
	 */
	public function send(){
	    //实例化相关表
	    $usident = M('user');
		//实例化相关model
		$seidentr= new MessageModel();
		//接受值
		$uid = I('post.uidval');
		if(IS_POST){
		    if(''==$uid){
		        $res = array('status'=>-1,'msg'=>'会员编号不能为空');
		        exit(json_encode($res));
		    }
		    $coun = $usident->where("`us_uid`='".$uid."'")->count();
            if('0'==$coun){
                $res = array('status'=>-1,'msg'=>'会员信息不存在');
                exit(json_encode($res));
            }
		    $re = $seidentr->do_send($uid);
		    if('1'==$re){
		        $res= array('status'=>1,'msg'=>'站内短信发送成功'); // 返回结果状态
		        exit(json_encode($res));
		    }
		}else{
		    $this->display();
		}
    }
    /**
     +--------------------------------------------------------------------------
     * 实现获取发件箱列表的功能
     * @author:  XC跃
     * @date :2018-03-14
     +--------------------------------------------------------------------------
     */
    public function outBox(){
        $this->display();
    }
    public function ajaxOutBox(){
        //实例化相关表
        $meident = M('message');
        // 搜索条件
        $condition = array();
        I('type') ? $condition['me_type'] = I('type') : false;
        I('title') ? $condition['me_title'] = array('LIKE',"%".I('title')."%") : false;
        I('uid') ? $condition['me_suid'] = array('LIKE',"%".I('uid')."%") : false;
        $condition['me_fuid']  = session('admin_auth.usname');
        $condition['me_fstate']= '1';
        //dump($condition);
        $sort_order = I('order_by').' '.I('sort');
        //echo $sort_order;
        $count = $meident->where($condition)->count();
        $Page  = new AjaxPage($count,50);
        $show = $Page->show();
        $list = $meident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
        //echo $meident->getLastSql();
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('rows',$Page->totalRows);
        $this->display();
    }
    /**
     +--------------------------------------------------------------------------
     * 实现获取收件箱列表的功能
     * @author:  XC跃
     * @date :2018-03-14
     +--------------------------------------------------------------------------
     */
    public function inBox(){
        $this->display();
    }
    public function ajaxInBox(){
        //实例化相关表
        $meident = M('message');
        // 搜索条件
        $condition = array();
        I('type') ? $condition['me_type'] = I('type') : false;
        I('title') ? $condition['me_title'] = array('LIKE',"%".I('title')."%") : false;
        I('uid') ? $condition['me_fuid'] = array('LIKE',"%".I('uid')."%") : false;
        $condition['me_suid']  = session('admin_auth.usname');
        $condition['me_sstate']= '1';
        //dump($condition);
        $sort_order = I('order_by').' '.I('sort');
        //echo $sort_order;
        $count = $meident->where($condition)->count();
        $Page  = new AjaxPage($count,50);
        $show = $Page->show();
        $list = $meident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
        //echo $usident->getLastSql();
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('rows',$Page->totalRows);
        $this->display();
    }
    /**
     +--------------------------------------------------------------------------
     * 实现管理员进行短信回复的功能
     * @author:  XC跃
     * @date :2018-03-14
     +--------------------------------------------------------------------------
     */
    public function smsReply(){
        $mid = I('id/d');
        //实例化相关表
        $meident = M('message');
        //实例化相关Model
        $meidentr= new MessageModel();
        
        if(IS_POST){
            $rema = I('post.reamrk');
            //1、不可重复回复
            $reFind= $meident->where("`me_id`='".$mid."'")->find();
            if(!empty($reFind['me_remark'])){
                $res= array('status'=>-1,'msg'=>'信息已回复，不可重复操作'); // 返回结果状态
                exit(json_encode($res));
            }
            //2、重新发送短信
            $sms = array(
                'title' => '反馈回复：'.$reFind['me_title'],
                'body'  => $rema,
                'suid' => $reFind['me_fuid'],
                'fuid' => $reFind['me_suid'],
            );
            $meidentr->sms_send($sms);
            //3、跟新回复内容            
            $re = $meident->where("`me_id`='".$mid."'")->save(array('me_remark'=>$rema,'me_slook'=>1));
            if('1'==$re){
                $res= array('status'=>1,'msg'=>'回复成功'); // 返回结果状态
                exit(json_encode($res));
            }
        }else{
            $mre = $meident->where("`me_id`='".$mid."'")->find();
            $this->assign('mre',$mre);
            $this->display();
        }
    }
    /**
     +--------------------------------------------------------------------------
     * 实现短信标记为已读功能
     * @author:  XC跃
     * @date :2018-03-14
     +--------------------------------------------------------------------------
     */
    public function smsLook(){
        $id  = I('post.id');
        $aid = session('admin_auth.usname');
        if(empty($id)){
            $res = array('status'=>-1,'msg'=>'未选中需要删除数据');
            exit(json_encode($res));
        }
        $ids = implode(",",$id);
        $re = M('message')->where("`me_suid`='".$aid."' and `me_id` in(".$ids.")")->save(array('me_slook'=>1));
        //echo M('message')->getLastSql();
        if($re>0){
            $res = array('status'=>1,'msg'=>'标记成功');
            exit(json_encode($res));
        }
    }
}